﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace DAO
{
    public class DataProvider
    {
        OleDbDataAdapter da;
        OleDbConnection con;
        DataTable table;

        public DataProvider()
        {
            KetNoi();
        }

        public void KetNoi()
        {
            string strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = DBQLNhaSach.mdb";
            con = new OleDbConnection(strCon);
            con.Open();
            con.Close();
        }
        public DataTable LayBangDuLieu(string sql)
        {
            if (con.State == ConnectionState.Closed)
                con.Open();
            table = new DataTable();
            OleDbCommand cmd = new OleDbCommand(sql, con);
            da = new OleDbDataAdapter(cmd);
            da.Fill(table);
            if (con.State == ConnectionState.Open)
                con.Close();
            return table;
        }
        public bool ThucHienCauTruyVan(string sql)
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                OleDbCommand cmd = new OleDbCommand(sql, con);
                cmd.ExecuteNonQuery();
                if (con.State == ConnectionState.Open)
                    con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public OleDbCommand Getcmd(string sql)
        {
            OleDbCommand cmd = new OleDbCommand(sql, con);
            return cmd;
        }

    }
}
